//
//  ReportCradAnswerSheePageVC.m
//  jzyParents
//
//  Created by 任辰东 on 2025/3/6.
//

#import "ReportCradAnswerSheePageVC.h"
#import "ReportCradAnswerSheetListVC.h"
#import "ReportCardKemuModel.h"
#import "ReportApis.h"
#import "UIImage+color.h"
#import "UIButton+BackgroundColor.h"

@interface ReportCradAnswerSheePageVC ()
@property (nonatomic, strong) ReportCardModel *model;
@property (nonatomic, strong) NSArray <ReportCardKemuModel *>*menuArray;
@property (nonatomic, copy) NSString *kemu_id;
@end

@implementation ReportCradAnswerSheePageVC

-(instancetype)initWithModel:(ReportCardModel *)model WithKemuID:(nonnull NSString *)kemu_id
{
    self = [super init];
    if (self) {
        _kemu_id = kemu_id;
        _model = model;
    }
    return self;
    
}

- (void)getMenuData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"main_id"] = self.model.main_id;
//    params[@"main_id"] = @"333";
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis ctb_Xq_KaoshiXuekeListRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        NSArray *data = [ReportCardKemuModel mj_objectArrayWithKeyValuesArray:responseData];
        [data enumerateObjectsUsingBlock:^(ReportCardKemuModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
              
            if ([obj.kemu_id isEqualToString:self.kemu_id]) {
                self.selectIndex = (int)idx;
            }
        }];
        self.menuArray = [NSArray arrayWithArray:data];
        [self reloadData];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

- (void)viewDidLoad {
    
    self.showOnNavigationBar = NO;
    self.pageAnimatable = YES;
    self.progressColor = UIColorFromHex(kColorMain);
    self.titleColorNormal = UIColorFromHex(0x999999);
    self.titleColorSelected = UIColorFromHex(kColorMain);
    self.menuView.backgroundColor = [UIColor whiteColor];
//    self.menuView. = 50;
    self.postNotification = YES;
    self.bounces = YES;
    self.menuViewStyle = WMMenuViewStyleDefault;
    self.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
    self.titleSizeSelected = 16;
    self.titleSizeNormal = 15;
    self.automaticallyCalculatesItemWidths = YES;
//    self.menuItemWidth = 20;
    self.itemMargin = 15.0;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configView];
    [self getMenuData];
    
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController
{
    return self.menuArray.count;
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    
    ReportCardKemuModel *model = self.menuArray[index];
    ReportCradAnswerSheetListVC *showVC = [[ReportCradAnswerSheetListVC alloc]initWithMainModel:self.model WithKemuModel:model];
    return showVC;
}


- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index
{
    ReportCardKemuModel *model = self.menuArray[index];
    return model.kemu_name;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    
    CGFloat height = 40;
    CGFloat originY = 0;
    return CGRectMake(0, originY, self.view.frame.size.width, height);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}

-(void)configView
{
    self.navigationItem.title = @"答题卡";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(kColorMain) size:CGSizeMake(kScreenWidth, kNavitionBarHeight)] forBarMetrics:UIBarMetricsDefault];
    
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:UIColorFromHex(kColorLine) size:CGSizeMake(kScreenWidth, kOnePixel)]];
    
    if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *navigationBarAppearance = [[UINavigationBarAppearance alloc]init];
            [navigationBarAppearance configureWithOpaqueBackground];
            [navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(kColorMain) size:CGSizeMake(kScreenWidth, kNavitionBarHeight)]];
            navigationBarAppearance.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorFromHex(kColorWhite),NSFontAttributeName:UIFontSize(21)};
            [navigationBarAppearance setShadowImage:[UIImage imageWithColor:UIColorFromHex(kColorMain) size:CGSizeMake(kScreenWidth, kOnePixel)]];
            self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
            self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
        }
    
    if (self.navigationController.viewControllers.count > 1) {
        [self setNavLeftItemWithImage:UIImageWithName(@"back_white") highlightImg:nil];
    }
    if (self.tabBarController == nil && kKeyWindow.rootViewController != self.navigationController) {
        [self setNavLeftItemWithImage:UIImageWithName(@"back_white") highlightImg:nil];
    }
    
    //防止自定义返回按钮后返回手势失效
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

@end
